package studentdemo.dao;

import studentdemo.modul.Class;
import studentdemo.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ClassDaoImpl implements ClassDao {
    @Override
    public int insertClass(Class clazz) {
        String sql = "INSERT INTO Dujx_Class11 (djx_Clno11, djx_Clname11, djx_Clmno11) VALUES (?, ?, ?)";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, clazz.getClno());
            ps.setString(2, clazz.getClname());
            ps.setString(3, clazz.getClmno());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateClass(Class clazz) {
        String sql = "UPDATE Dujx_Class11 SET djx_Clname11=?, djx_Clmno11=? WHERE djx_Clno11=?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, clazz.getClname());
            ps.setString(2, clazz.getClmno());
            ps.setString(3, clazz.getClno());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteClass(String clno) {
        String sql = "DELETE FROM Dujx_Class11 WHERE djx_Clno11=?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, clno);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Class getClassById(String clno) {
        String sql = "SELECT * FROM Dujx_Class11 WHERE djx_Clno11=?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, clno);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return new Class(rs.getString("djx_Clno11"), rs.getString("djx_Clname11"), rs.getString("djx_Clmno11"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Class> getClassesByMajor(String clmno) {
        String sql = "SELECT * FROM Dujx_Class11 WHERE djx_Clmno11=? ORDER BY djx_Clno11";
        List<Class> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, clmno);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Class(rs.getString("djx_Clno11"), rs.getString("djx_Clname11"), rs.getString("djx_Clmno11")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Class> getAllClasses() {
        String sql = "SELECT * FROM Dujx_Class11 ORDER BY djx_Clno11";
        List<Class> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection(); Statement st = conn.createStatement()) {
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                list.add(new Class(rs.getString("djx_Clno11"), rs.getString("djx_Clname11"), rs.getString("djx_Clmno11")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
} 